package app.fugema.shop.helper;

import android.graphics.Bitmap;
import android.text.Html;
import android.view.View;

import androidx.annotation.DrawableRes;

import com.base.common.app.LoginUtils;
import com.base.common.permission.PermissionUtils;
import com.base.common.utils.JavaMethod;
import com.base.common.utils.LogUtil;
import com.base.common.utils.UIUtils;
import com.base.common.view.base.BaseActivity;
import com.jeremyliao.liveeventbus.LiveEventBus;
import app.fugema.shop.R;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMVideo;
import com.umeng.socialize.media.UMWeb;

import java.io.File;
import java.lang.ref.WeakReference;

import io.reactivex.functions.Consumer;


public class ShareHelper {
    private ShareAction shareAction;
    private ShareBuilder builder;

    private ShareHelper(ShareBuilder builder) {
        this.builder = builder;
        initShareAction();
    }

    private void initShareAction()
    {
        if (shareAction == null) {
            shareAction = new ShareAction(builder.activityWeakReference.get());
//                    .setDisplayList(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,SHARE_MEDIA.SINA);
        }
    }

    public void share() {
        PermissionUtils.initStoragePermission(builder.activityWeakReference.get())
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            startShare();
                        } else {
                            UIUtils.showToastSafes("授权失败");
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        if (throwable.getMessage() != null) {

                        }
                    }
                }).isDisposed();
    }


    private void startShare() {
        initShareAction();
        if (builder instanceof LinkBuilder) {
            LinkBuilder linkBuilder = (LinkBuilder) builder;
            if (linkBuilder.shareLink.isEmpty()) {
                UIUtils.showToastSafes("分享链接不能为空");
                return;
            }
            UMWeb umWeb = new UMWeb(linkBuilder.shareLink);
            umWeb.setTitle(Html.fromHtml(linkBuilder.shareTitle).toString());
            umWeb.setDescription(Html.fromHtml(linkBuilder.shareDesc).toString());
            UMImage umImage;
            if (linkBuilder.iconUrl != null && !linkBuilder.iconUrl.isEmpty()) {
                umImage = new UMImage(builder.activityWeakReference.get(), linkBuilder.iconUrl);
            } else {
                umImage = new UMImage(builder.activityWeakReference.get(), linkBuilder.iconId);
            }
            umWeb.setThumb(umImage);

            shareAction.withMedia(umWeb);
        } else if (builder instanceof ImageBuilder) {
            ImageBuilder imageBuilder = (ImageBuilder) builder;
            UMImage umImg;
            if (imageBuilder.shareImgUrl != null && !imageBuilder.shareImgUrl.isEmpty()) {
                umImg = new UMImage(builder.activityWeakReference.get(), imageBuilder.shareImgUrl);
                umImg.setThumb(umImg);
            } else if (imageBuilder.shareImgFile != null) {
                umImg = new UMImage(builder.activityWeakReference.get(), imageBuilder.shareImgFile);
                umImg.setThumb(umImg);
            } else if (imageBuilder.bitmap != null) {
                umImg = new UMImage(builder.activityWeakReference.get(), imageBuilder.bitmap);
                umImg.setThumb(umImg);
            } else {
                UIUtils.showToastSafes("分享图片为空或者不存在");
                return;
            }
            shareAction.withMedia(umImg);
        } else if (builder instanceof VideoBuilder) {
            VideoBuilder videoBuilder = (VideoBuilder) builder;

            UMVideo video = new UMVideo(videoBuilder.shareVideoUrl);
            video.setTitle(videoBuilder.shareTitle);//视频的标题
            video.setThumb(new UMImage(builder.activityWeakReference.get(), videoBuilder.sharePosterUrl));//视频的缩略图
            video.setDescription(videoBuilder.shareDesc);//视频的描述

            shareAction.withMedia(video);
        } else {
            throw new IllegalArgumentException("必须继承LinkBuilder或者ImageBuilder");
        }


        if (!UMShareAPI.get(builder.activityWeakReference.get()).isInstall(builder.activityWeakReference.get(), builder.sharePlatform)) {
            UIUtils.showToastSafes("没有安装该应用");
            return;
        }
        shareAction
                .setCallback(builder.listener)
                .setPlatform(builder.sharePlatform)
                .share();
//                .open();
    }

    public static class SimpleUMShareListener implements UMShareListener {


        @Override
        public void onStart(SHARE_MEDIA share_media) {
//            ToastUtils.toast("分享中...");
            LogUtil.d("ShareHelper", "分享中...");
        }

        @Override
        public void onError(SHARE_MEDIA share_media, Throwable throwable) {
//            ToastUtils.toast("分享失败");
            LogUtil.d("ShareHelper", "分享失败");
        }

        @Override
        public void onCancel(SHARE_MEDIA share_media) {
//            ToastUtils.toast("分享取消");
            LogUtil.d("ShareHelper", "分享取消");
        }

        @Override
        public void onResult(SHARE_MEDIA share_media) {
//            ToastUtils.toast("分享成功");
            LogUtil.d("ShareHelper", "分享成功");
            LiveEventBus.get("ShareHelper", String.class).post("成功");
        }

    }

    public static class LinkBuilder extends ShareBuilder {
        private String shareTitle = "";
        private String shareDesc = "";
        private String shareLink = "";
        private boolean isAdditional = false; //是否有附加的功能按钮 举报 收藏
        private boolean isShowEdit = false;//是否显示编辑

        public LinkBuilder(BaseActivity context, String shareLink) {
            super(context);

            shareLink = JavaMethod.changeParamForKey(shareLink, "userId", String.valueOf(LoginUtils.getUserId()));
            this.shareLink = shareLink;
        }

        public LinkBuilder title(String title) {
            this.shareTitle = title;
            return this;
        }

        public LinkBuilder description(String desc) {
            this.shareDesc = desc;
            return this;
        }

        public boolean isShowEdit() {
            return isShowEdit;
        }

        public LinkBuilder setShowEdit(boolean showEdit) {
            isShowEdit = showEdit;
            return this;
        }

        public boolean isAdditional() {
            return isAdditional;
        }

        public LinkBuilder setAdditional(boolean additional) {
            isAdditional = additional;
            return this;
        }

        public String getShareLink()
        {
            return shareLink;
        }
    }

    public static class VideoBuilder extends ShareBuilder {
        private String shareVideoUrl = "";
        private String sharePosterUrl = "";//略缩图
        private String shareTitle = "";
        private String shareDesc = "";


        public VideoBuilder(BaseActivity context, String shareVideoUrl, String sharePosterUrl) {
            super(context);
            this.shareVideoUrl = shareVideoUrl;
            this.sharePosterUrl = sharePosterUrl;
        }

        public VideoBuilder title(String shareTitle) {
            this.shareTitle = shareTitle;
            return this;
        }

        public VideoBuilder desc(String shareDesc) {
            this.shareDesc = shareDesc;
            return this;
        }
    }

    public static class ImageBuilder extends ShareBuilder {
        private String shareImgUrl = "";
        private File shareImgFile;
        private Bitmap bitmap;

        public ImageBuilder(BaseActivity context, String shareImgUrl) {
            super(context);
            this.shareImgUrl = shareImgUrl;
        }

        public ImageBuilder(BaseActivity context, File shareImgFile) {
            super(context);
            this.shareImgFile = shareImgFile;
        }

        public ImageBuilder(BaseActivity context, Bitmap bitmap) {
            super(context);
            this.bitmap = bitmap;
        }
    }

    public static abstract class ShareBuilder {
        protected String iconUrl = "";
        protected @DrawableRes
        int iconId = R.mipmap.ic_launcher;
        protected SHARE_MEDIA sharePlatform;
        protected UMShareListener listener = new SimpleUMShareListener();
        protected View.OnClickListener clickListener;
        private WeakReference<BaseActivity> activityWeakReference;

        public ShareBuilder(BaseActivity context)
        {
            this.activityWeakReference = new WeakReference<>(context);
        }

        public ShareBuilder icon(String url) {
            if (url != null && !url.isEmpty()) {
                this.iconUrl = url;
            }
            return this;
        }

        public ShareBuilder icon(@DrawableRes int resId) {
            this.iconId = resId;
            return this;
        }

        public ShareBuilder platform(SHARE_MEDIA sharePlatform) {
            this.sharePlatform = sharePlatform;
            return this;
        }

        public ShareBuilder listener(UMShareListener listener) {
            this.listener = listener;
            return this;
        }

        public ShareBuilder setClickListener(View.OnClickListener clickListener) {
            this.clickListener = clickListener;
            return this;
        }

        public View.OnClickListener getClickListener() {
            return clickListener;
        }

        public ShareHelper build() {
            return new ShareHelper(this);
        }
    }
}
